<template>
  <div class="ms-list">
    <el-row class="operation">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/' }">
          <icon style="" name="home"></icon>
        </el-breadcrumb-item>
        <el-breadcrumb-item>客户管理</el-breadcrumb-item>
        <el-breadcrumb-item>问卷管理</el-breadcrumb-item>
        <el-breadcrumb-item>答卷详情</el-breadcrumb-item>
        <el-breadcrumb-item>列表</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>

    <div ref="msSearch">
      <!--@clickH="calculatedHeight"-->
      <ms-search :data="search.list" @click="updateConditions"/>
    </div>

    <ms-slide
      :title="survey.title"
      :width="600" @close="closeSlide()" ref="slideModifyRow">
      <br>
      <div style="margin-left: 10px;" v-for="(value, key, index) in survey.answer" :key="key">
        <div>
          Q{{(index + 1) + " : " + key}}
          <span v-if="value.typeId === 1">【单选题】</span>
          <span v-else-if="value.typeId === 2">【多选题】</span>
          <span v-else-if="value.typeId === 3">【填空题】</span>
          <span v-else>【NPS题】</span>
        </div>
        <div style="margin-left: 10px;">{{value.answer}}</div>
        <hr style="height:1px;border:none;border-top:1px solid #5111;"/>
      </div>
    </ms-slide>

    <el-table
      v-loading="table.loading"
      size="small" stripe border
      :data="table.list"

      :fit="true">
      <el-table-column prop="surveyTitle" label="问卷标题" min-width="150" fixed></el-table-column>
      <el-table-column prop="senderName" label="发放者" min-width="60"></el-table-column>
      <el-table-column prop="visti" label="访问量" min-width="85" align="right"
                       header-align="center"></el-table-column>
      <el-table-column prop="userName" label="填写者姓名" min-width="60"></el-table-column>
      <el-table-column prop="mobile" label="填写者电话" min-width="100"></el-table-column>
      <el-table-column prop="ip" label="填写者IP" min-width="100"></el-table-column>
      <el-table-column prop="ipProvince" label="填写者所在省份" min-width="100"></el-table-column>
      <el-table-column prop="ipCity" label="填写者所在城市" min-width="100"></el-table-column>
      <el-table-column prop="duration" label="填写时长(分钟)" min-width="85"></el-table-column>
      <el-table-column prop="finishTime" label="填写完成时间" min-width="140"></el-table-column>
      <el-table-column prop="firstVistiTime" label="首次访问时间" min-width="140"></el-table-column>
      <el-table-column label="操作" fixed="right" min-width="100">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click.native.prevent="getResult(scope.$index)">答题详情</el-button>
          <el-button type="text" size="small" @click.native.prevent="download(scope.$index)">导出</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      class="pagination"
      layout="total, sizes, prev, pager, next, jumper"
      :current-page.sync="table.pagination.page"
      :total="table.pagination.totalRow"
      :page-size="table.pagination.pageSize"
      :page-sizes="table.pagination.pageSizes"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
  import MsSearch from '@/components/common/Search.vue'
  import MsSlide from '@/components/common/Slide.vue'
  import Api from '@/api/customer/platform/survey-result'
  import { SEARCH_CONDITION } from '@/models/customer/platform/survey-result'
  import ResponseParse from '@/utils/response-parse'

  export default {
    components: {
      MsSearch,
      MsSlide
    },
    data () {
      return {
        search: {
          list: [],
          condition: {}
        },
        table: {
          list: [],
          pagination: {},
          loading: true
        },
        survey: {
          title: '',
          answer: {}
        }
//        PHeigh: document.documentElement.clientHeight - 120,
//        tbHeight: 100
      }
    },
//    mounted: function () {
//      this.$nextTick(function () { this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight) })
//    },
    created () {
      this.search.list = SEARCH_CONDITION
      this.table.list = []
      this.requestList()
    },
    methods: {
      searchCriteria (val, index) {
        this.$set(this.search.list[index], 'value', val)
      },
//      calculatedHeight () {
//        this.$nextTick(function () {
//          this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight)
//        })
//      },
      requestList () {
        this.table.loading = true
        const search = Object.assign({}, this.pagination, this.search.condition)
        Api.list({search: search}, response => {
          let status = response.status || 0
          let body = response.data || []
          if (status === 200 || status === 204) {
            this.table.list = body
            this.table.pagination = ResponseParse.pagination(response.headers)
//            if (this.table.pagination.totalPage === this.table.pagination.page) {
//              this.tbHeight = ''
//            } else {
//              this.$set(this, 'tbHeight', this.PHeigh - this.$refs.msSearch.clientHeight)
//            }
          } else {
//            this.tbHeight = ''
            this.table.list = []
            this.$message.error(body.error || '数据获取失败')
          }
          this.table.loading = false
        })
      },
      getResult (index) {
        this.survey.title = this.table.list[index].surveyTitle
        const id = this.table.list[index].id
        Api.listAnswer({id: id}, response => {
          let status = response.status || 0
          let body = response.data || {}
          if (status === 200 || status === 204) {
            this.survey.answer = body.map
            this.table.list[index].visti = body.list[0].visti
            this.table.list[index].firstVistiTime = body.list[0].firstVistiTime
            this.showSlide('slideModifyRow')
          } else {
            this.$message.error(body.error || '答题详情获取失败')
          }
        })
      },
      download (index) {
        const id = this.table.list[index].id
        window.location.href = '/rest/customer/platform/survey-results/' + id + '/files'
      },
      handleSizeChange (val) {
        this.table.pagination.pageSize = val
        this.requestList()
      },
      handleCurrentChange (val) {
        this.table.pagination.page = val
        this.requestList()
      },
      updateConditions (conditions) {
        this.table.pagination.page = 1
        this.search.condition = conditions
        this.requestList()
      },
      showSlide (ref) {
        this.$refs[ref].show()
      },
      closeSlide () {
        this.survey = {title: '', answer: {}}
      }
    }
  }
</script>

<style lang="scss" scoped>

</style>
